from lxml import etree

from util import http_utils
from util.g import g
from util.selenium.webdriver.common.by import By
from base.BaseMethod import BM

class IARiprIec():

    form_btn_loc = (By.LINK_TEXT, '知识产权对企业竞争力的作用')
    ripr_iec_loc = (By.CSS_SELECTOR, 'textarea#Zscqjz')
    save_btn_loc = (By.CSS_SELECTOR, 'button#dataEprCycxSave')

    def __init__(self, response, driver=None, text=''):
        self.response = response
        self.driver = driver
        self.text = text

    def fill_form(self):
        add_url = "https://gqqy.chinatorch.org.cn/xonlinereport/inforeport/DataInnocom/saveOrUpdateDataEprCycx.do"
        data = {
            "dataeprcycx.id": "",
            "dataInnocom.id": "",
            "dataEprCycx.dataId": "",
            "dataEprCycx.entId": "",
            "subType": "",
            "type": "",
            "struts.token.name": "token",
            "token": self.response.get("token"),
            "dataEprCycx.createDate": "",
            "dataEprCycx.Czscqjz": self.text,
        }
        res_text = self.response.get("text")
        tree = etree.HTML(res_text)
        cycx_id_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycx.id']/@value"))
        id_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataInnocom.id']/@value"))
        dataId_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycx.dataId']/@value"))
        entId_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycx.entId']/@value"))
        subType_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycxForm_subType']/@value"))
        type_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycxForm_type']/@value"))
        createDate_res = BM.get_first(tree.xpath("//form[@id='dataEprCycxForm']/input[@id='dataEprCycxForm_dataEprCycx_createDate']/@value"))
        data["dataeprcycx.id"] = cycx_id_res
        data["dataInnocom.id"] = id_res
        data["dataEprCycx.dataId"] = dataId_res
        data["dataEprCycx.entId"] = entId_res
        data["subType"] = subType_res
        data["type"] = type_res
        data["dataEprCycx.createDate"] = createDate_res

        res = http_utils.send_post(add_url, data=data, driver=self.driver)
        message = res.get("message")
        g.logger.info("知识产权对企业竞争力的作用 上传结果: {}".format(message))
        if u"成功" in message:
            return True
        else:
            return False


